package com.gali.leetcode.day1;

import java.util.HashMap;

/**
 * @Auther: gali
 * @Date: 2022/7/14-07-14-21:18
 * @Description:
 */
public class AddSum {
    public int[] twoSum(int[] nums, int target) {
        //哈希查找的时间复杂度为 O(1)O(1)O(1)，所以可以利用哈希容器 map 降低时间复杂度
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])){
                System.out.println(map.get(target-nums[i]));
                System.out.println(i);
                System.out.println("success");
                return new int[]{map.get(target-nums[i]),i};
        }
            map.put(nums[i],i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        AddSum addSum = new AddSum();
        int nums[]={1,2,3};
        addSum.twoSum(nums,5);
    }
}
